package com.yanqu.road.dao.impl.activity.stronghold;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.huashigang.HuaShiGangFloorConfig;
import com.yanqu.road.entity.activity.stronghold.config.*;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StrongholdActivityDaoImpl extends TempDao {
    public static String getStrongholdWildConfigSql = "select * from t_s_activity_stronghold_house where activityId = ?";

    public Map<Integer, StrongholdWildConfig> getStrongholdWildConfig(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, StrongholdWildConfig> configMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getStrongholdWildConfigSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdWildConfig strongholdWildConfig = new StrongholdWildConfig();
                strongholdWildConfig.setActivityId(rs.getInt("activityId"));
                strongholdWildConfig.setId(rs.getInt("id"));
                strongholdWildConfig.setName(rs.getString("name"));
                strongholdWildConfig.setType(rs.getInt("type"));
                strongholdWildConfig.setIcon(rs.getString("icon"));
                strongholdWildConfig.setSort(rs.getInt("sort"));
                strongholdWildConfig.setEventWeight(rs.getString("eventWeight"));
                String refreshBoxConfigStr = rs.getString("refreshBoxConfig");
                Map<Integer, List<Integer>> addBoxMap = new HashMap<>();
                int index = 0;
                for (String str : refreshBoxConfigStr.split("\\|")) {
                    String[] arr = str.split(";");
                    List<Integer> addNumList = new ArrayList<>();
                    addNumList.add(Integer.parseInt(arr[0]));
                    addNumList.add(Integer.parseInt(arr[1]));
                    addNumList.add(Integer.parseInt(arr[2]));
                    addBoxMap.put(index, addNumList);
                    index++;
                }
                strongholdWildConfig.setAddBoxMap(addBoxMap);
                configMap.put(strongholdWildConfig.getId(), strongholdWildConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }

    public static String getStrongholdDesertConfigSql = "select * from t_s_activity_desert_floor where activityId = ?";

    public Map<Integer, HuaShiGangFloorConfig> getStrongholdDesertConfig(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, HuaShiGangFloorConfig> configMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getStrongholdDesertConfigSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                HuaShiGangFloorConfig strongholdDesertConfig = new HuaShiGangFloorConfig();
                strongholdDesertConfig.setActivityId(rs.getInt("activityId"));
                strongholdDesertConfig.setFloor(rs.getInt("floor"));
                strongholdDesertConfig.setSpecialReward(rs.getString("specialReward"));
                strongholdDesertConfig.setRewards(rs.getString("rewards"));
                strongholdDesertConfig.setConsume(rs.getString("consume"));
                configMap.put(strongholdDesertConfig.getFloor(), strongholdDesertConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }

    public static String getStrongholdDonateConfigSql = "select * from t_s_activity_stronghold_base where activityId = ?";

    public Map<Integer, StrongholdDonateConfig> getStrongholdDonateConfig(int activityId){
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, StrongholdDonateConfig> configMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getStrongholdDonateConfigSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdDonateConfig strongholdDonateConfig = new StrongholdDonateConfig();
                strongholdDonateConfig.setActivityId(rs.getInt("activityId"));
                strongholdDonateConfig.setId(rs.getInt("id"));
                strongholdDonateConfig.setIcon(rs.getString("icon"));
                strongholdDonateConfig.setLevel(rs.getInt("level"));
                strongholdDonateConfig.setHpAdd(rs.getInt("hpAdd"));
                strongholdDonateConfig.setNeedProcess(rs.getString("needProcess"));
                configMap.put(strongholdDonateConfig.getLevel(), strongholdDonateConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }

    public static String getStrongholdNpcConfigSql = "select * from t_s_activity_stronghold_npc where activityId = ?";

    public Map<Integer, StrongholdNpcConfig> getStrongholdNpcConfig(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, StrongholdNpcConfig> configMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getStrongholdNpcConfigSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdNpcConfig strongholdNpcConfig = new StrongholdNpcConfig();
                strongholdNpcConfig.setActivityId(rs.getInt("activityId"));
                strongholdNpcConfig.setId(rs.getInt("id"));
                strongholdNpcConfig.setAppearanceId(rs.getString("npcId"));
                strongholdNpcConfig.setWeight(rs.getInt("weight"));
                strongholdNpcConfig.setNeedMoney(rs.getString("needMoney"));
                strongholdNpcConfig.setRewards(rs.getString("rewards"));
                configMap.put(strongholdNpcConfig.getId(), strongholdNpcConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }

    public static String getStrongholdTimeConfigSql = "select * from t_s_activity_stronghold_time where activityId = ?";

    public Map<Integer, StrongholdTimeConfig> getStrongholdTimeConfig(int activityId)  {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, StrongholdTimeConfig> configMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getStrongholdTimeConfigSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdTimeConfig strongholdTimeConfig = new StrongholdTimeConfig();
                strongholdTimeConfig.setActivityId(rs.getInt("activityId"));
                strongholdTimeConfig.setTimeId(rs.getInt("timeId"));
                strongholdTimeConfig.setType(rs.getInt("type"));
                strongholdTimeConfig.setBeginTime(rs.getInt("beginTime"));
                strongholdTimeConfig.setDurationTime(rs.getInt("durationTime"));
                strongholdTimeConfig.setParam(rs.getString("param"));
                configMap.put(strongholdTimeConfig.getTimeId(), strongholdTimeConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }

    public static String getStrongholdTradeConfigSql = "select * from t_s_activity_stronghold_trade where activityId = ?";

    public Map<Integer, StrongholdTradeConfig> getStrongholdTradeConfig(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, StrongholdTradeConfig> configMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getStrongholdTradeConfigSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                StrongholdTradeConfig strongholdTradeConfig = new StrongholdTradeConfig();
                strongholdTradeConfig.setActivityId(rs.getInt("activityId"));
                strongholdTradeConfig.setId(rs.getInt("id"));
                strongholdTradeConfig.setName(rs.getString("name"));
                strongholdTradeConfig.setDesc(rs.getString("desc"));
                strongholdTradeConfig.setConsume(rs.getString("consume"));
                strongholdTradeConfig.setRewards(rs.getString("rewards"));
                strongholdTradeConfig.setRankRange(rs.getString("rankRange"));
                configMap.put(strongholdTradeConfig.getId(), strongholdTradeConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }


    public Map<String, String> getStrongholdParamConfigMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<String, String> configMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            String sql = "select * from t_s_activity_stronghold_config where activityId = ?";
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                String param = rs.getString("param");
                String value = rs.getString("value");
                configMap.put(param, value);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return configMap;
    }
}
